package ${serviceImplPackagePath};

import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ${servicePackagePath}.${entityName?cap_first}Service;
import ${repositoryPackagePath}.${entityName?cap_first}Repository;
import ${boPackagePath}.${entityName?cap_first}Bo;
import ${poPackagePath}.${entityName?cap_first}Po;
import ${searchBoPackagePath}.${entityName?cap_first}SearchBo;
import ${searchPoPackagePath}.${entityName?cap_first}SearchPo;
import ${innerTransferUtilPackagePath}.InnerTransferUtil;

@Slf4j
@Service
public class ${entityName?cap_first}ServiceImpl implements ${entityName?cap_first}Service {

    @Autowired
    private ${entityName?cap_first}Repository ${entityName?uncap_first}Repository;


    @Override
    public List<${entityName?cap_first}Bo> list(${entityName?cap_first}SearchBo searchBo) {
        if(searchBo == null){
            log.info("查询条件不能为null");
            return null;
        }

        ${entityName?cap_first}SearchPo searchPo = InnerTransferUtil.transferBean(searchBo, ${entityName?cap_first}SearchPo.class);
        List<${entityName?cap_first}Po> ${entityName?uncap_first}PoList = ${entityName?uncap_first}Repository.list(searchPo);
        if(${entityName?uncap_first}PoList == null || ${entityName?uncap_first}PoList.size() == 0) {
            log.info("未查询到数据，searchPo: {}", searchPo);
            return null;
        }
        List<${entityName?cap_first}Bo> boList = new ArrayList<>();
        for(${entityName?cap_first}Po po : ${entityName?uncap_first}PoList) {
            boList.add(InnerTransferUtil.transferBean(po, ${entityName?cap_first}Bo.class));
        }
        return boList;
    }

    @Override
    public void insert(${entityName?cap_first}Bo ${entityName?uncap_first}Bo) {
        log.info("入参:{}", ${entityName?uncap_first}Bo);
        StringBuilder errorMsg = new StringBuilder();
        checkParam(${entityName?uncap_first}Bo,errorMsg);
        if (errorMsg.length() > 0) {
            log.error(errorMsg.toString());
            // throw new Exception(errorMsg.toString());
        }
        ${entityName?uncap_first}Repository.insert(InnerTransferUtil.transferBean(${entityName?uncap_first}Bo, ${entityName?cap_first}Po.class));
    }

    @Override
    public void insertBatch(List<${entityName?cap_first}Bo> ${entityName?uncap_first}BoList) {
        log.info("批量添加入参:{}", ${entityName?uncap_first}BoList);
        StringBuilder errorMsg = new StringBuilder();
        for (${entityName?cap_first}Bo ${entityName?uncap_first}Bo : ${entityName?uncap_first}BoList) {
            checkParam(${entityName?uncap_first}Bo,errorMsg);
        }

        if (errorMsg.length() > 0) {
            log.error(errorMsg.toString());
            // throw new Exception(errorMsg.toString());
        }
        List<${entityName?cap_first}Po> ${entityName?uncap_first}PoList = new ArrayList<>();
        for(${entityName?cap_first}Bo ${entityName?uncap_first}Bo : ${entityName?uncap_first}BoList) {
            ${entityName?uncap_first}PoList.add(InnerTransferUtil.transferBean(${entityName?uncap_first}Bo, ${entityName?cap_first}Po.class));
        }
        ${entityName?uncap_first}Repository.insertBatch(${entityName?uncap_first}PoList);
    }

    /**
     * 参数检查
     */
    private void checkParam(${entityName?cap_first}Bo ${entityName?uncap_first}Bo, StringBuilder errorMsg) {

    <#list classFieldInfoList as classFieldInfo>
        <#if classFieldInfo.type.code == "String">
        if (StringUtils.isBlank(${entityName?uncap_first}Bo.get${classFieldInfo.name?cap_first}() )) {
            errorMsg.append("不能为空");
        }
        </#if>
    </#list>

    }

    @Override
    public void modify(${entityName?cap_first}Bo ${entityName?uncap_first}Bo) {
        log.info("修改数据入参:{}", ${entityName?uncap_first}Bo);
        if (${entityName?uncap_first}Bo.getId() == null) {
            log.error("id不能为空");
            //throw new Exception("id不能为空");
        }
        StringBuilder errorMsg = new StringBuilder();
        checkParam(${entityName?uncap_first}Bo, errorMsg);
        if (errorMsg.length() > 0) {
            log.error(errorMsg.toString());
            //throw new Exception(errorMsg.toString());
        }
        ${entityName?cap_first}Po ${entityName?uncap_first}Po = ${entityName?uncap_first}Repository.selectById(${entityName?uncap_first}Bo.getId());
        if (${entityName?uncap_first}Po == null) {
            log.error("修改数据不存在");
            //throw new Exception("修改数据不存在");
        }
        ${entityName?uncap_first}Repository.update(InnerTransferUtil.transferBean(${entityName?uncap_first}Bo, ${entityName?cap_first}Po.class));
    }

    @Override
    public void deleteById(Long id) {
        ${entityName?uncap_first}Repository.deleteById(id);
    }
}
